package com.quartz.SpringBootQuartz.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.quartz.SpringBootQuartz.entity.Cron;
import com.quartz.SpringBootQuartz.entity.JobEntity;
import com.quartz.SpringBootQuartz.service.JobEntityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class JobEntityController {
    @Autowired
    private JobEntityService jobEntityService;
    @Value("${upload.filePath}")
    private String uploadFilePath;

    @RequestMapping(value = "/findByCriteria")
    public PageInfo<JobEntity> findByCriteria(JobEntity jobEntity, @RequestParam(defaultValue = "1") Integer pageNum , @RequestParam(defaultValue = "2") Integer pageSize)throws Exception{
        PageHelper.startPage(pageNum,pageSize);
        List<JobEntity> list = jobEntityService.findByCriteria(jobEntity);
        PageInfo<JobEntity> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @RequestMapping(value = "/findGroups")
    public List<String> findGroups()throws Exception{
        List<String> groups = jobEntityService.findGroups();
        return groups;
    }
    @RequestMapping(value = "/findStatus")
    public List<String> findStatus()throws Exception{
        return jobEntityService.findStatus();
    }
    @RequestMapping(value = "/deleteById/{id}")
    public Integer deleteById(@PathVariable Integer id)throws Exception{
        return jobEntityService.deleteById(id);
    }
    @RequestMapping(value = "/updateById")
    public String updateById( JobEntity jobEntity,MultipartFile jarPath_file){
        /*jarPath_file==null 不需要更新jarPath*/
        if (jarPath_file==null){
            try {
                Integer integer = jobEntityService.updateById(jobEntity);
                if (integer != 0)
                    return "UPDATE SUCCESS";
                else
                    return "UPDATE ERROR";
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (jarPath_file.isEmpty()) {
            return "上传失败,请选择文件";
        }
        String fileName = jarPath_file.getOriginalFilename();
        String filePath = uploadFilePath;
        File dest = new File(filePath + fileName);
        File delFile = new File(jobEntity.getJarPath().trim());
        try {
            jarPath_file.transferTo(dest);
            jobEntity.setJarPath(filePath+fileName);
            Integer integer = jobEntityService.updateById(jobEntity);
            if (integer != 0) {
                delFile.delete();
                return "UPDATE SUCCESS";
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "UPDATE ERROR";
    }

    @RequestMapping(value = "/add")
    public String add(JobEntity jobEntity, MultipartFile jarPath_file ){
        try {
            if (jobEntity != null && jobEntity.getId() != null ) {
                List<Integer> id = jobEntityService.findId(jobEntity.getId());
                if (id.size()>0)
                    return "ID不可用";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (jarPath_file==null){
            return "上传失败，请选择文件";
        }
        if (jarPath_file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = jarPath_file.getOriginalFilename();
        String filePath = uploadFilePath;
        File dest = new File(filePath + fileName);
        try {
            jarPath_file.transferTo(dest);
            jobEntity.setJarPath(filePath+fileName);
            Integer add = jobEntityService.add(jobEntity);
            if (add != 0)
                return "ADD SUCCESS";
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "ADD ERROR";
    }
    @RequestMapping(value = "/findCrons")
    public List<Cron> findCrons()throws Exception{
        return jobEntityService.findCrons();
    }
    @RequestMapping(value = "/findById")
    public List<JobEntity> findById(Integer id)throws Exception{
        return jobEntityService.findById(id);
    }
}
